﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wpf_Tool_ZC.TabControl_Z;
using Wpf_Tool_ZC.util;

namespace Wpf_Tool_ZC
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static MainWindow Self = null;

        public MainWindow()
        {
            InitializeComponent();

            Self = this;
        }

        private void Wnd_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Application.Current.Shutdown();
        }

        string FstrSelectPath = @"D:\ZC_RuanJian\movie\test01\小猪佩奇(持续更新)\小猪佩奇中文版 1080P\小猪佩奇中文版第7季 1080P";

        private void Click_SelectFolder(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "请选择文件夹";
            dialog.SelectedPath = FstrSelectPath;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    MessageBox.Show(this, "选择的文件夹路径为空", "提示");
                    return;
                }

                FstrSelectPath = dialog.SelectedPath;
                tbSelectedFolder.Text = FstrSelectPath;
            }
        }

        List<string> BianLiFile01(string _strPath)
        {
            //C#遍历指定文件夹中的所有文件 
            DirectoryInfo folder = new DirectoryInfo(_strPath);
            if (!folder.Exists)
            {
                MessageBox.Show("文件夹不存在", "提示");
                return null;
            }

            List<string> list = new List<string>();

            //遍历文件
            foreach (FileInfo file in folder.GetFiles())// folder.GetDirectories() 目录
                list.Add(file.Name);
            return list;
        }

        private void Click_BianLi(object sender, RoutedEventArgs e)
        {
            string strPath = tbSelectedFolder.Text.Trim();

            List<string> list = BianLiFile01(strPath);

            tbMsg.AppendText("文件总数：" + list.Count + "\r\n");
            for (int i=0; i< list.Count; i++)
                tbMsg.AppendText("文件["+i+"]：" + list[i] + "\r\n");
        }

        private void Click_aLink(object sender, RoutedEventArgs e)
        {
            string strPath = tbSelectedFolder.Text.Trim();
            tbMsg.AppendText("\r\n");

            List<string> list = BianLiFile01(strPath);

            string str1 = "<a href='play.html?小猪佩奇(持续更新)/小猪佩奇中文版 1080P/小猪佩奇中文版第7季 1080P/";
            string str2 = "' target='_blank'>";
            string str3 = "</a> <br/>";

            for (int i = 0; i < list.Count; i++)
                tbMsg.AppendText(str1 + list[i] + str2 + list[i] + str3 + "\r\n");
        }

        private void Click_ClearMsg(object sender, RoutedEventArgs e)
        {
            tbMsg.Clear();
        }

        private void Click_HuanJingBianLiang(object sender, RoutedEventArgs e)
        {
            WndHuanJingBianLiang wnd = new WndHuanJingBianLiang();
            wnd.ShowDialog();
        }

        public Wnd_Regedit FwndRegedit = null;

        private void Click_Regedit(object sender, RoutedEventArgs e)
        {
            if (FwndRegedit == null)
                FwndRegedit = new Wnd_Regedit();

            FwndRegedit.Show();
            FwndRegedit.Activate();
        }

        public Wnd_Log FwndLog = null;

        private void Click_Log(object sender, RoutedEventArgs e)
        {
            if (FwndLog == null)
                FwndLog = new Wnd_Log();

            FwndLog.Show();
            FwndLog.Activate();
        }

        public static void WndLog_AppendText(string _str)
        {
            if (Self.FwndLog != null)
                Self.FwndLog.MsgAppend(_str);
        }

        private void Click_MatlabBijiao(object sender, RoutedEventArgs e)
        {
            List<string> list1 = UtilFile.ReadTxt(@"C:\Users\Administrator\Desktop\Matlab_A.txt");
            List<string> list2 = UtilFile.ReadTxt(@"C:\Users\Administrator\Desktop\Matlab_B.txt");

            Console.WriteLine("list1.Count :{0}", list1.Count);
            Console.WriteLine("list2.Count :{0}", list2.Count);
            if (list1.Count != list2.Count)
                return;


            int iFalseMax = 0;
            int r1 = 0, c1 = 0;
            int iTrueMin = 255;
            int r2 = 0, c2 = 0;
            for (int r = 0; r < list1.Count; r++)
            {
                string[] strs1 = list1[r].Split('\t');
                string[] strs2 = list2[r].Split('\t');
                if (strs1.Length != strs2.Length)
                {
                    Console.WriteLine("strs1.Length :{0}", strs1.Length);
                    Console.WriteLine("strs2.Length :{0}", strs2.Length);
                    return;
                }

                for (int c = 0; c < strs1.Length; c++)
                {
                    if (strs1[c] == "false")
                    {
                        int i = Convert.ToInt16(strs2[c]);
                        if (iFalseMax < i)
                        {
                            iFalseMax = i;
                            r1 = r;
                            c1 = c;
                        }
                    }

                    if (strs1[c] == "true")
                    {
                        int i = Convert.ToInt16(strs2[c]);
                        if (iTrueMin > i)
                        {
                            iTrueMin = i;
                            r2 = r;
                            c2 = c;
                        }
                    }
                }
            }

            Console.WriteLine("iFalseMax: {0}, r1: {1}, c1: {2}", iFalseMax, r1, c1);
            Console.WriteLine("iTrueMin: {0}, r2: {1}, c2: {2}", iTrueMin, r2, c2);

            Console.WriteLine("normal exit");
        }

        private void Click_Shot(object sender, RoutedEventArgs e)
        {
            Wnd_Shot wndShot = new Wnd_Shot();
            wndShot.Show();
        }

        private void Click_string(object sender, RoutedEventArgs e)
        {
            Wnd_String wndString = new Wnd_String(FwndLog);
            wndString.Show();
        }

        private void Click_TabControlWnd(object sender, RoutedEventArgs e)
        {
            Wnd_TabControl wndTabControl = new Wnd_TabControl();
            wndTabControl.Show();
        }

        private void Click_WndZa(object sender, RoutedEventArgs e)
        {
            WndZa w = new WndZa();
            w.ShowDialog();
        }
    }
}
